package com.hmkj.web.controller.pay;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hmkj.core.api.lianlian.PartnerConfig;
import com.hmkj.core.api.lianlian.PayDataBean;
import com.hmkj.core.api.lianlian.RetBean;
import com.hmkj.core.api.lianlian.util.YinTongUtil;
import com.hmkj.core.constant.ConfigNID;
import com.hmkj.core.po.amount.Recharge;
import com.hmkj.core.service.amount.CashService;
import com.hmkj.core.service.amount.RechargeService;
import com.hmkj.web.controller.GenericController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@RestController
@RequestMapping(GenericController.BASE_API_URL + "/notify/lianlian")
public class LianLianPayNotifyController extends GenericController<Integer, Recharge>{
    @Resource
    private RechargeService rechargeService;
    @Resource
    private CashService cashService;

    /**
     * 同步回调通知
     */
    @ResponseBody
    @RequestMapping(value = "/doReturn", method = RequestMethod.POST)
    public void doReturn(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        resp.setCharacterEncoding("UTF-8");
        System.out.println("进入支付同步通知数据接收处理");
        String reqStr = YinTongUtil.readReqStr(req);
        JSONObject jsonOb = YinTongUtil.getJson(reqStr);
        String url = redisService.get(ConfigNID.PAY_RETURN_URL);//返回的ip地址
        if (YinTongUtil.isnull(reqStr)) {
            System.out.println("支付同步通知数据接收处理失败");
            resp.sendRedirect(url + "/doReturn.html?result_pay=FAIL");
        } else {
            System.out.println("接收支付同步通知数据：【" + reqStr + "】");
            System.out.println("支付同步通知数据接收处理成功");
            resp.sendRedirect(url + "/doReturn.html?result_pay=" + jsonOb.get("result_pay").toString() + "&money_order=" + jsonOb.get("money_order").toString());
        }
    }

    /**
     * 支付成功后异步回调
     */
    @ResponseBody
    @RequestMapping(value = "/doNotify", method = RequestMethod.POST)
    public void doNotify(HttpServletRequest req, HttpServletResponse resp) throws Exception {

        resp.setCharacterEncoding("UTF-8");
        RetBean retBean = new RetBean();
        String reqStr = YinTongUtil.readReqStr(req);
        if (YinTongUtil.isnull(reqStr)) {
            retBean.setRet_code("9999");
            retBean.setRet_msg("交易失败");
            log.info("连连支付异步通知，未接收到处理数据");
        } else {
            try {
                if (!YinTongUtil.checkSign(reqStr, PartnerConfig.YT_PUB_KEY, PartnerConfig.MD5_KEY)) {
                    retBean.setRet_code("9999");
                    retBean.setRet_msg("交易失败");
                    log.info("连连支付验签失败，通知数据："+reqStr);
                } else {
                    log.info("连连支付验签成功，通知数据："+reqStr);
                    retBean.setRet_code("0000");
                    retBean.setRet_msg("交易成功");
                    resp.getWriter().write(JSON.toJSONString(retBean));
                    resp.getWriter().flush();
                    // 解析异步通知对象
                    PayDataBean payDataBean = JSON.parseObject(reqStr, PayDataBean.class);
                    //DO 增加用户总资金，可用资金   充值记录表状态改为通过
                    rechargeService.handleRechargeByLianLian(payDataBean);
                }
            } catch (Exception e) {
                log.info("连连支付异步通知处理异常：" + e);
                retBean.setRet_code("9999");
                retBean.setRet_msg("交易失败");
            }
        }
    }


    /**
     * 代付支付成功后异步回调(出金提现异步回调)
     */
    @ResponseBody
    @RequestMapping(value = "/dfNotify", method = RequestMethod.POST)
    public void dfNotify(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        resp.setCharacterEncoding("UTF-8");
        String reqStr = getRequestParams(req);
        RetBean retBean = new RetBean();
//        String reqStr = YinTongUtil.readReqStr(req);
        if (YinTongUtil.isnull(reqStr)) {
            retBean.setRet_code("9999");
            retBean.setRet_msg("交易失败");
            System.out.println("返回参数为空");
        } else {
            try {
                if (!YinTongUtil.checkSign(reqStr, PartnerConfig.YT_PUB_KEY, PartnerConfig.MD5_KEY)) {
                    retBean.setRet_code("9999");
                    retBean.setRet_msg("交易失败");
                    System.out.println("支付异步通知验签失败");
                } else {
                    // 解析异步通知对象
                    PayDataBean payDataBean = JSON.parseObject(reqStr, PayDataBean.class);
                    // 增加用户总资金，可用资金 ，冻结资金减少    充值记录表状态改为通过
                    cashService.handleOrderByLianLian(payDataBean);
                    retBean.setRet_code("0000");
                    retBean.setRet_msg("交易成功");
                    resp.getWriter().write(JSON.toJSONString(retBean));
                    resp.getWriter().flush();
                    System.out.println("支付异步通知数据接收处理成功");
                }
            } catch (Exception e) {
                System.out.println("异步通知报文解析异常：" + e);
                retBean.setRet_code("9999");
                retBean.setRet_msg("交易失败");
            }
        }
    }

}
